home *** CD-ROM | disk | FTP | other *** search
Text File | 2010-01-29 | 37.7 KB | 1,187 lines |
- if(!com) var com={};
- if(!com.VidBar) com.VidBar={};
-
- com.VidBar.Observer = {
- //ChromeBase : "chrome://vidbar/",
-
- registered : false,
- cleanOnQuit : false,
- observe : function(subject, topic, data) {
- //alert("topic = " + topic);
- if (topic == "quit-application-requested") {
- var cancel = subject.QueryInterface(Components.interfaces.nsISupportsPRBool);
- if(cancel==true){
- com.VidBar.__d("com.VidBar.Observer: shutdown has been cancelled by another component");
- return;
- }
- var result = com.VidBar.VidDownloader.doCloseCheck();
- if (result.cancel) {
- com.VidBar.__d("com.VidBar.Observer: cancel shutdown");
- //alert("com.VidBar.Observer: cancel shutdown");
- // cancel shutdown
- cancel.data = true;
- //alert("com.VidBar.Observer: cancelled shutdown");
- } else {
- //alert("com.VidBar.Observer: clear list = "+result.clear);
- if(result.clear){
- com.VidBar.Observer.cleanOnQuit = true;
- com.VidBar.VidDownloader.cleanAll();
- }
- }
- } else if (topic == "quit-application-granted") {
- //alert("com.VidBar.Observer.cleanOnQuit = "+com.VidBar.Observer.cleanOnQuit);
- if(com.VidBar.Observer.cleanOnQuit){
- com.VidBar.__d("com.VidBar.Observer: clean downloadings");
- //alert("com.VidBar.Observer: clean downloadings");
- com.VidBar.VidDownloader.cleanAll();
- //alert("com.VidBar.Observer: downloadings are cleaned");
- }
- com.VidBar.Observer.unregister();
- } else if (topic == "em-action-requested") {
- com.VidBar.__d("com.VidBar.Observer: em-action-requested");
- var toolbarId = com.VidBar.Pref.getToolbarId();
- subject.QueryInterface(Components.interfaces.nsIUpdateItem);
- if (subject.id == toolbarId) {
- if(data == "item-uninstalled") {
- com.VidBar.__d("com.VidBar.Observer: item-uninstalled");
- com.VidBar.Pref.clearFirstTime();
- } else if (data == "item-cancel-action") {
- com.VidBar.Pref.unsetFirstTime();
- }else if (data == "item-installed") {
- com.VidBar.__d("com.VidBar.Observer: item-installed");
- com.VidBar.Main.checkFirstStart();
- //com.VidBar.ServerPings.onInstall();
- }else if (data == "item-upgraded") {
- com.VidBar.__d("com.VidBar.Observer: item-upgraded");
- //com.VidBar.Main.setFirstStart();
- com.VidBar.ServerPings.onInstall();
- }
- }
-
- }
- },
- register : function() {
- if(this.registered)
- return;
-
- com.VidBar.__d("com.VidBar.Observer.register");
- var observerService = Components.classes["@mozilla.org/observer-service;1"]
- .getService(Components.interfaces.nsIObserverService);
-
- observerService.addObserver(this, "quit-application-granted", false);
- observerService.addObserver(this, "quit-application-requested", false);
- observerService.addObserver(this, "em-action-requested", false);
- },
- unregister : function() {
- if(!this.registered)
- return;
-
- com.VidBar.__d("com.VidBar.Observer.unregister");
- var observerService = Components.classes["@mozilla.org/observer-service;1"]
- .getService(Components.interfaces.nsIObserverService);
-
- observerService.removeObserver(this, "quit-application-granted");
- observerService.removeObserver(this, "quit-application-requested");
- observerService.removeObserver(this, "em-action-requested");
- }
- };
-
- com.VidBar.UI = {
- toolbarDownloadButton : null,
- toolbarSysMenu : null,
- toolbarDownloadMenu : null,
-
- navToolbarButton : null,
- navToolbarContextMenu : null,
- navToolbarDownloadMenu : null,
-
- // statusbarButton : null,
- // statusbarMenu : null,
- // statusbarContextMenu : null,
-
- // toolsMenu : null,
- contextMenu : null,
-
- autofill : null,
-
- init : function(pref) {
- com.VidBar.__d("com.VidBar.UI.init");
- this.pref = pref;
- this.pref.registerObserver(["show-in-context-menu", "icon-pos", "layout-type"], this);
-
- this.install();
-
- this.toolbarDownloadButton = document.getElementById("vidbar-download-button");
- this.toolbarSysMenu = this.initSystemMenu("vidbar-sys-menupopup");
- this.toolbarDownloadMenu = document.getElementById("vidbar-download-menupopup");
-
- this.navToolbarButton = document.getElementById("vidbtn-toolbar-button");
- this.navToolbarContextMenu = this
- .initSystemMenu("vidbtn-toolbar-ctxmenupopup");
- this.navToolbarDownloadMenu = document.getElementById("vidbtn-download-menupopup");
-
- // this.statusbarButton = document.getElementById("vidbar-statusbar-button");
- // this.statusbarMenu = document.getElementById("vidbar-statusbar-menupopup");
- // this.statusbarContextMenu = this
- // .initSystemMenu("vidbar-statusbar-ctxmenupopup");
-
- // this.toolsMenu = this.initSystemMenu("vidbar-toolsmenupopup");
- this.contextMenu = this.initSystemMenu("vidbar-ctxmenupopup");
-
- this.updateContextMenu();
- this.updateLayout();
- // this.updateIconPos();
- // this.autofill = new com.VidBar.AutoFill();
- },
- observePref : function(data) {
- if (data == "show-in-context-menu")
- this.updateContextMenu();
- else if (data == "layout-type")
- this.updateLayout();
- // else if (data == "icon-pos")
- // this.updateIconPos();
- },
- initSystemMenu : function(id) {
- var menu = document.getElementById(id);
- this.clearMenu(menu);
- this.generateSystemMenu(menu);
- return menu;
- },
- install : function() {
- com.VidBar.__d("com.VidBar.UI.install");
- try {
- var vidId = "vidbtn-toolbaritem";
- var afterId = "urlbar-container";
- var afterElem = document.getElementById(afterId);
- if (afterElem) {
- var navBar = afterElem.parentNode;
- if (document.getElementById(vidId) == null) {
- navBar.insertItem(vidId, afterElem);
- navBar.setAttribute("currentset", navBar.currentSet);
- document.persist("nav-bar", "currentset");
- }
- }
- } catch (e) {
- com.VidBar.__e("com.VidBar.UI.install: " + e);
- }
- },
- enableButton : function(button) {
- button.setAttribute("disabled", "false");
- if (this.pref.getIconStyle() == "3D") {
- button.setAttribute("class", "on");
- } else {
- button.setAttribute("class", "on-noanim");
- }
- },
- enableVidButton : function() {
- this.enableButton(this.toolbarDownloadButton);
- this.enableButton(this.navToolbarButton);
- // this.enableButton(this.statusbarButton);
- },
- disableButton : function(button) {
- button.setAttribute("disabled", "true");
- button.setAttribute("class", "off");
- },
- disableVidButton : function() {
- this.disableButton(this.toolbarDownloadButton);
- this.disableButton(this.navToolbarButton);
- // this.disableButton(this.statusbarButton);
- },
- clearMenu : function(menupopup) {
- while (menupopup.firstChild) {
- menupopup.removeChild(menupopup.firstChild);
- }
- },
- generateSystemMenu : function(menupopup) {
- var doc = menupopup.ownerDocument;
- var _this = this;
- var menuitem;
- menuitem = doc.createElement("menuitem");
- menuitem.setAttribute("label", "Preferences");
- menuitem.setAttribute("image", "chrome://vidbar/skin/preferences.png");
- menuitem.setAttribute("class", "menuitem-iconic");
- menuitem.addEventListener("command", function() {
- com.VidBar.UI.preferences();
- }, false);
- menupopup.appendChild(menuitem);
-
- // menuitem = doc.createElement("menuitem");
- // menuitem.setAttribute("label", "Supported Sites");
- // menuitem.addEventListener("command", function() {
- // com.VidBar.UI.openSites();
- // }, false);
- // menupopup.appendChild(menuitem);
-
- menuitem = doc.createElement("menuitem");
- menuitem.setAttribute("label", "Downloads");
- menuitem.setAttribute("image", "chrome://vidbar/skin/downloads.gif");
- menuitem.setAttribute("class", "menuitem-iconic");
- menuitem.addEventListener("command", function() {
- com.VidBar.UI.openDownloadQueue();
- }, false);
- menupopup.appendChild(menuitem);
-
- menuitem = doc.createElement("menuitem");
- menuitem.setAttribute("label", "Open Download Folder");
- menuitem.setAttribute("image", "chrome://vidbar/skin/open_download_folder.gif");
- menuitem.setAttribute("class", "menuitem-iconic");
- menuitem.addEventListener("command", function() {
- com.VidBar.UI.openDownloadDir();
- }, false);
- menupopup.appendChild(menuitem);
-
- menuitem = doc.createElement("menuitem");
- menuitem.setAttribute("label", "Help");
- menuitem.setAttribute("image", "chrome://vidbar/skin/help2.gif");
- menuitem.setAttribute("class", "menuitem-iconic");
- menuitem.addEventListener("command", function() {
- com.VidBar.UI.help();
- }, false);
- menupopup.appendChild(menuitem);
-
- menuitem = doc.createElement("menuseparator");
- menupopup.appendChild(menuitem);
-
- menuitem = doc.createElement("menuitem");
- menuitem.setAttribute("label", "About");
- menuitem.setAttribute("image", "chrome://vidbar/skin/about-logo-16.gif");
- menuitem.setAttribute("class", "menuitem-iconic");
- menuitem.addEventListener("command", function() {
- com.VidBar.UI.about();
- }, false);
- menupopup.appendChild(menuitem);
- },
- about : function() {
- com.VidBar.__d("com.VidBar.UI.about");
- var options = "chrome,centerscreen,modal";
- window.openDialog("chrome://vidbar/content/about.xul", "vidbar-dialog",
- options, {});
- },
- openHome : function(){
- com.VidBar.__d("com.VidBar.UI.openHome");
- this.openURLInNewTab("http://ffpimp.com/");
- },
- openInbox : function(){
- com.VidBar.__d("com.VidBar.UI.openInbox");
- var url = this.pref.getInboxUrl();
- if(!url){
- alert("Inbox URL is not set. Please set it in Preferences.");
- this.preferences();
- return;
- }
- this.openURLInNewTab(url);
- },
- openTranslate : function(){
- com.VidBar.__d("com.VidBar.UI.openTranslate");
- var urlTranslate = this.getCurrentDoc();
- var url = "http://www.microsofttranslator.com/BV.aspx?ref=zugo&from=&to=en&a=" + encodeURIComponent(urlTranslate.URL);
- this.openURLInNewTab(url);
- },
- openStumble : function(){
- com.VidBar.__d("com.VidBar.UI.openStumble");
- var url="http://www.stumbleupon.com/s/";
- this.openURLInNewTab(url);
- },
- openFacebook : function(){
- com.VidBar.__d("com.VidBar.UI.openFacebook");
- this.openURLInNewTab("http://www.facebook.com/");
- },
- searchBing : function(){
- com.VidBar.__d("com.VidBar.UI.searchBing");
- var keyword=document.getElementById("vidbar-search-box").value;
- var url="http://bing.zugotoolbar.com/s/?q="+encodeURIComponent(keyword)+"&site=Bing&pid=87";
- this.openURLInNewTab(url);
- },
- searchBingVideo : function(){
- com.VidBar.__d("com.VidBar.UI.searchBingVideo");
- var keyword=document.getElementById("vidbar-search-box").value;
- var url="http://bing.zugotoolbar.com/s/?q="+encodeURIComponent(keyword)+"&site=Bing&pid=87&engine=video";
- this.openURLInNewTab(url);
- },
- onSearchBoxKeyPressed : function(event) {
- if (event.keyCode == event.DOM_VK_RETURN) {
- this.searchBing();
- }
- },
- help : function() {
- com.VidBar.__d("com.VidBar.UI.help");
- content.location.href = "http://vids.st/help/";
- },
- preferences : function() {
- com.VidBar.__d("com.VidBar.UI.preferences");
- var options = "chrome,centerscreen,modal";
- window.openDialog("chrome://vidbar/content/preferences.xul", "vidbar-dialog",
- options, {});
-
- },
- openSites : function() {
- com.VidBar.__d("com.VidBar.UI.openSites");
- var w = window.open("chrome://vidbar/content/sites.xul", "vidbar-sites",
- "chrome,centerscreen,resizable=yes");
- w.focus();
- },
- openDownloadQueue : function() {
- com.VidBar.__d("com.VidBar.UI.openDownloadQueue");
- var w = window.open("chrome://vidbar/content/queue.xul",
- "vidbar-download-queue", "chrome,centerscreen,resizable=yes");
- w.focus();
- },
- openDownloadDir : function() {
- com.VidBar.__d("com.VidBar.UI.openDownloadDir");
- try {
- var dir = this.pref.getDownloadDirectory()
- .QueryInterface(Components.interfaces.nsILocalFile);
- dir.reveal();
- } catch (e) {
- com.VidBar.__e("com.VidBar.UI.openDownloadDir: " + e);
- }
- },
- openURLInNewTab : function(url) {
- com.VidBar.__d("com.VidBar.UI.openURLInNewTab");
- var browser = window.getBrowser();
- var tab = browser.addTab(url);
-
- setTimeout(function(b, t) {
- b.selectedTab = t;
- }, 0, browser, tab);
- },
- getCurrentDoc : function() {
- var browser = gBrowser
- .getBrowserAtIndex(gBrowser.mTabContainer.selectedIndex);
- return browser.contentDocument;
- },
- isCurrentDoc : function(doc) {
- return doc == this.getCurrentDoc();
- },
- update : function(rep, doc) {
- if (!doc)
- doc = this.getCurrentDoc();
- else if (!this.isCurrentDoc(doc))
- return;
-
- this.updateMenu(rep, doc);
- },
- updateMenu : function(rep, doc) {
- com.VidBar.__d("com.VidBar.UI.updateMenu");
- var mediaList = rep.getMediaListByDoc(doc);
-
- try {
- this.updateMediaMenu(this.toolbarDownloadMenu, mediaList);
- this.updateMediaMenu(this.navToolbarDownloadMenu, mediaList);
- // this.updateMediaMenu(this.statusbarMenu, mediaList);
- this.updateMediaMenu(this.contextMenu, mediaList, true);
-
- if (mediaList.length == 0) {
- this.disableVidButton();
- } else {
- this.enableVidButton();
- }
- } catch (e) {
- com.VidBar.__e("com.VidBar.UI.updateMenu: " + e);
- }
- },
- updateMediaMenu : function(menu, mediaList, checkSep) {
- var doc = menu.ownerDocument;
-
- if (checkSep) {
- var sep = doc.getElementById("VidCtxMediaSep");
- if (mediaList.length > 0) {
- if (sep == null) {
- var menusep = doc.createElement("menuseparator");
- menusep.setAttribute("id", "VidCtxMediaSep");
- menu.appendChild(menusep);
- }
- } else {
- if (sep != null) {
- menu.removeChild(sep);
- }
- }
- }
-
- var items = [];
- for (var i = 0; i < mediaList.length; i++) {
- var media = mediaList[i];
- var menuitem = null;
- for (var j = 0; j < menu.childNodes.length; j++) {
- var node = menu.childNodes[j];
- if (node && node.nodeName == "menuitem" && node.mediaData) {
- if (node.mediaData.guid == media.guid) {
- menuitem = node;
- break;
- }
- }
- }
- var label = media.label;
- if (label.length > 60) {
- var dp = label.lastIndexOf(".");
- var ext = label.substr(dp);
- label = label.substr(0, 60 - ext.length - 2) + ".." + ext;
- }
- if (media.size) {
- label += " - " + VidUtils.getSizeStr(media.size);
- }
-
- if (menuitem) {
- menuitem.setAttribute("label", label);
- // menuitem.setAttribute("image", "chrome://vidbar/skin/"
- // + media.type + ".gif");
- this.updateMenuItemTooltip(doc, "tip-" + media.guid, media);
- menuitem.mediaData = media;
- } else {
- menuitem = doc.createElement("menuitem");
- menuitem.setAttribute("label", label);
- // menuitem.setAttribute("image", "chrome://vidbar/skin/"
- // + media.type + ".gif");
- menuitem.setAttribute("image", "chrome://vidbar/skin/vid16-on.png");
- var ttid = this.createMenuItemTooltip(doc, media);
- menuitem.setAttribute("tooltip", ttid);
- menuitem.setAttribute("class", "menuitem-iconic");
- var _this = this;
- menuitem.addEventListener("command", function(event) {
- _this.menuItemClicked(event);
- }, true);
- menuitem.mediaData = media;
-
-
- // Create context menu
- var popup = doc.createElement("popup");
- popup.setAttribute("id", "popup-menu");
- var menupopupitem = doc.createElement("menuitem");
- menupopupitem.setAttribute("label", " Save Video As..");
- menupopupitem.setAttribute("class", "menuitem-iconic");
- menupopupitem.mediaData = media;
- menupopupitem.addEventListener("command", function(event) {
- _this.menuPopupItemClicked(event);}, true);
- popup.appendChild(menupopupitem);
- doc.documentElement.appendChild(popup);
- menuitem.setAttribute("context", "popup-menu");
- menu.setAttribute("context", "popup-menu");
-
- menu.appendChild(menuitem);
- }
- var statusDisplay = VidUtils.getDownloadStatusDisplay(media.status);
- menuitem.setAttribute("style", statusDisplay[1]);
- items.push(menuitem);
- }
- for (var j = 0; j < menu.childNodes.length; j++) {
- var node = menu.childNodes[j];
- if (!node.mediaData)
- continue;
-
- var has = false;
- for (var i = 0; i < items.length; i++) {
- if (items[i] == node) {
- has = true;
- break;
- }
- }
- if (!has) {
- menu.removeChild(node);
- var tooltip = document.getElementById("tip-"
- + node.mediaData.guid);
- if (tooltip) {
- tooltip.parentNode.removeChild(tooltip);
- }
- }
- }
- },
- createMenuItemTooltip : function(doc, media) {
- var tooltip = doc.createElement("tooltip");
- var ttid = "tip-" + media.guid;
- tooltip.setAttribute("id", ttid);
- this.updateMenuItemTooltip(doc, ttid, media);
- doc.documentElement.appendChild(tooltip);
- return ttid;
- },
- updateMenuItemTooltip : function(doc, ttid, media) {
- var tooltip = doc.getElementById(ttid);
- if (!tooltip)
- return;
-
- while (tooltip.firstChild) {
- tooltip.removeChild(tooltip.firstChild);
- }
-
- var statusDisplay = VidUtils.getDownloadStatusDisplay(media.status);
-
- var label;
- label = doc.createElement("label");
- label.setAttribute("value", media.label);
- label.setAttribute("style", "font-weight:bold;" + statusDisplay[1]);
- tooltip.appendChild(label);
-
- label = doc.createElement("label");
- label.setAttribute("value", "URL: " + media.mediaUrl);
- tooltip.appendChild(label);
-
- label = doc.createElement("label");
- label.setAttribute("value", "Page: " + media.pageUrl);
- tooltip.appendChild(label);
-
- label = doc.createElement("label");
- label.setAttribute("value", "Size: " + VidUtils.getSizeStr(media.size));
- tooltip.appendChild(label);
-
- if (media.status) {
- label = doc.createElement("label");
- label.setAttribute("value", "Status: " + statusDisplay[0]);
- tooltip.appendChild(label);
-
- if (media.status == VidStatus.DOWNLOADING
- || media.status == VidStatus.PAUSED) {
- if (media.percentComplete && media.percentComplete >= 0) {
- label = doc.createElement("label");
- label.setAttribute("value", "Percent Complete: "
- + media.percentComplete + "%");
- tooltip.appendChild(label);
- }
-
- if (media.amountTransferred) {
- label = doc.createElement("label");
- label
- .setAttribute(
- "value",
- "Downloaded Size: "
- + VidUtils
- .getSizeStr(media.amountTransferred));
- tooltip.appendChild(label);
- }
- if (media.status == VidStatus.DOWNLOADING && media.speed) {
- label = doc.createElement("label");
- label
- .setAttribute("value", "Speed: "
- + VidUtils.getSizeStr(media.speed)
- + "/sec");
- tooltip.appendChild(label);
- }
- }
- }
- },
- menuItemClicked : function(e) {
- com.VidBar.__d("com.VidBar.UI.menuItemClicked");
- if (e.originalTarget.nodeName == "menuitem") {
- var data = e.originalTarget.mediaData;
- if (data) {
- // com.VidBar.__d("menu item clicked: " + data.filename);
- switch (data.status) {
- case VidStatus.NOTDOWNLOADED :
- com.VidBar.VidDownloader.queueDownload(data);
- break;
- case VidStatus.ENQUEUED :
- case VidStatus.DOWNLOADING :
- break;
- case VidStatus.COMPLETE :
- // TODO: open video or dir
- break;
- case VidStatus.FAILED :
- // TODO: prompt for retry
- break;
- case VidStatus.CANCELLED :
- // TODO: prompt for redownload
- break;
- case VidStatus.PAUSED :
- // TODO: prompt for conintue
- break;
- case VidStatus.QUEUED :
- break;
- case VidStatus.BLOCKED :
- case VidStatus.SCANNING :
- default :
- break;
- }
- }
- }
- },
- menuPopupItemClicked : function(e) {
- com.VidBar.__d("com.VidBar.UI.menuPopupItemClicked");
- if (e.originalTarget.nodeName == "menuitem") {
- var data = e.originalTarget.mediaData;
- if (data) {
- netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
- netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
- var nsIFilePicker = Components.interfaces.nsIFilePicker;
- var nsILocalFile = Components.interfaces.nsILocalFile;
- nsILocalFile = this.pref.getDownloadDirectory();
- var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
- fp.init(window, " Save Video As..", nsIFilePicker.modeSave);
- var tempFilename = data.filename.substring(0, data.filename.length-4);
- fp.defaultString = tempFilename;
- fp.displayDirectory = nsILocalFile;
- fp.appendFilter("Video files (*.flv)", "*.flv");
- var rv = fp.show();
- if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
- data.filename = fp.file.leafName +'.'+ data.extension;
- data.dir = fp.displayDirectory.path;
- com.VidBar.VidDownloader.queueDownload(data);
- }
-
- }
- }
- },
- updateContextMenu : function() {
- com.VidBar.__d("com.VidBar.UI.updateContextMenu");
- var show = this.pref.isContextMenuShown();
- document.getElementById("vidbar-ctxmenu").setAttribute("hidden",
- "" + !show);
- },
- updateLayout : function() {
- com.VidBar.__d("com.VidBar.UI.updateLayout");
- var layout = this.pref.getLayoutType();
- com.VidBar.__d("layout: "+layout);
- document.getElementById("vidbtn-toolbaritem").setAttribute("hidden",
- "" + layout == "toolbar");
- document.getElementById("vidbar-toolbar").setAttribute("hidden",
- "" + layout == "button");
- },
- updateIconPos : function() {
- com.VidBar.__d("com.VidBar.UI.updateIconPos");
- var pos = this.pref.getIconPos();
- // document.getElementById("vidbar-statusbarpanel").setAttribute("hidden",
- // "" + pos == "toolbar");
- document.getElementById("vidbar-toolbaritem").setAttribute("hidden",
- "" + pos == "statusbar");
- },
- autoFill : function(){
- this.autofill.autoFill();
- },
- editAutofillProfile : function(){
- this.autofill.editProfile();
- }
- };
-
- com.VidBar.Repository = {
- list : [],
- observers : [],
- processors : [],
- init : function() {
- com.VidBar.__d("com.VidBar.Repository.init");
- },
- addObserver : function(ob) {
- this.observers.push(ob);
- },
- getTopDoc : function(doc) {
- if (doc && doc.defaultView && doc.defaultView.top)
- return doc.defaultView.top.document;
- else
- return null;
- },
- update : function(doc) {
- com.VidBar.__d("com.VidBar.Repository.update");
- doc = this.getTopDoc(doc);
- if (doc) {
- for (var i = 0; i < this.observers.length; i++) {
- if (this.observers[i]) {
- this.observers[i].update(this, doc);
- }
- }
- }
- },
- addMediaListByDoc : function(list, doc) {
- com.VidBar.__d("com.VidBar.Repository.addMediaListByDoc");
- if (list.length > 0) {
- doc = this.getTopDoc(doc);
- if (!doc || !doc.URL)
- return;
- com.VidBar.__d("com.VidBar.Repository.addMediaListByDoc\ndoc: " + doc.URL
- + "\nlist length: " + list.length);
- var mediaList = this.getMediaListByDoc(doc);
- for (var i = 0; i < list.length; i++)
- {
- if (list[i] == null || typeof(list[i]) == "undefined")
- continue;
-
- var found = false;
- for (var j = 0; j < mediaList.length; j++) {
- if (mediaList[j].mediaUrl == list[i].mediaUrl) {
- for (var a in list[i]) {
- mediaList[j][a] = list[i][a];
- }
- found = true;
- break;
- }
- }
- if (!found) {
- list[i].doc = doc;
- list[i].guid = VidUtils.generateGuid();
- list[i].status = VidStatus.NOTDOWNLOADED;
-
- this.list.push(list[i]);
- }
- }
- this.update(doc);
- }
- },
- getMediaListByDoc : function(doc) {
- com.VidBar.__d("com.VidBar.Repository.getMediaListByDoc: - doc: " + doc.URL);
- var list = [];
- doc = this.getTopDoc(doc);
- if (!doc)
- return list;
-
- for (var i = 0; i < this.list.length; i++) {
- if (this.list[i].doc == doc)
- list.push(this.list[i]);
- }
- // com.VidBar.__d("com.VidBar.Repository.getMediaListByDoc\ndoc: " + doc.URL + "\nlist
- // length: " + list.length);
- return list;
- },
- removeMediaByDoc : function(doc) {
- com.VidBar.__d("com.VidBar.Repository.removeMediaByDoc");
- var list = [];
- doc = this.getTopDoc(doc);
- if (!doc)
- return;
-
- for (var i = 0; i < this.list.length; i++) {
- if (this.list[i].doc != doc)
- list.push(this.list[i]);
- }
- // com.VidBar.__d("com.VidBar.Repository.removeMediaByDoc\ndoc: " + doc.URL + "\nold list
- // length: " + this.list.length + "\nnew list length: " + list.length);
- this.list = list;
- this.update(doc);
- },
- updateMediaByGuid : function(guid, data) {
- com.VidBar.__d("com.VidBar.Repository.updateMediaByGuid");
- var media = this.getMediaByGuid(guid);
- if (media) {
- for (var i in data) {
- if (i != "doc")
- media[i] = data[i];
- }
- if (media.doc) {
- this.update(media.doc);
- }
- }
- },
- getMediaByGuid : function(guid) {
- com.VidBar.__d("com.VidBar.Repository.getMediaByGuid");
- var media = null;
- for (var i = 0; i < this.list.length; i++) {
- if (this.list[i].guid == guid) {
- media = this.list[i];
- }
- }
- return media;
- }
- };
-
- com.VidBar.VidDownloader = {
- onDownloadStateChange : function(aState, aDownload) {
- },
- onStateChange : function(webProgress, request, stateFlags, status,
- aDownload) {
- var entry = this.db.getDownloadingById(aDownload.id);
- if (entry) {
- if (stateFlags
- & Components.interfaces.nsIWebProgressListener.STATE_STOP) {
-
- if (aDownload.state == Components.interfaces.nsIDownloadManager.DOWNLOAD_FINISHED){
- this.transferDone(entry, aDownload);
- } else {
- this.updateDownloadStatus(entry, VidStatus.translateState(aDownload.state));
- }
- } else {
- this.updateDownloadStatus(entry, VidStatus.DOWNLOADING);
- }
- }
- },
- onProgressChange : function(webProgress, request, curSelfProgress,
- maxSelfProgress, curTotalProgress, maxTotalProgress, aDownload) {
- var entry = this.db.getDownloadingById(aDownload.id);
- if (entry) {
- this.updateDownloadProgress(entry, aDownload);
- }
- },
- onStatusChange : function(webProgress, request, status, message, aDownload) {
- },
- onLocationChange : function(webProgress, request, location, aDownload) {
- },
- onSecurityChange : function(webProgress, request, state, aDownload) {
- },
- init : function(pref, repository) {
- com.VidBar.__d("com.VidBar.VidDownloader.init");
- try {
- this.pref = pref;
- this.pref.registerObserver(["max-downloads"], this);
- this.repository = repository;
- this.db = new com.VidBar.VidDB();
- this.db.checkDBFormat();
- setTimeout(function(){
- com.VidBar.VidDownloader.initDownloadManager();
- }, 0);
- } catch (e) {
- com.VidBar.__e("com.VidBar.VidDownloader.init: " + e);
- }
- },
- initDownloadManager : function(){
- com.VidBar.__d("com.VidBar.VidDownloader.initDownloadManager");
- try{
- // this.dlMgr = gDownloadManager;
- this.dlMgr = Components.classes["@mozilla.org/download-manager;1"]
- .getService(Components.interfaces.nsIDownloadManager);
- this.dlMgr.addListener(this);
- this.syncDownloads();
- this.checkDownloads();
- this.checkDownloadQueue();
- }catch(ex){
- com.VidBar.__e("com.VidBar.VidDownloader.initDownloadManager: " + e);
- }
- },
- close : function() {
- this.dlMgr.removeListener(this);
- },
- observePref : function(data) {
- if (data == "max-downloads")
- this.checkDownloadQueue();
- },
- checkDownloads : function() {
- },
- queueDownload : function(data) {
- com.VidBar.__d("com.VidBar.VidDownloader.queueDownload");
- try {
- var entry = {
- guid : data.guid,
- filename : data.filename,
- dir : data.dir,
- mediaUrl : data.mediaUrl,
- referrer : data.referrer,
- size : data.size,
- status : VidStatus.QUEUED
- };
-
- this.repository.updateMediaByGuid(data.guid, {
- status : VidStatus.QUEUED
- });
-
- this.db.addWaiting(data.guid, data.mediaUrl, data.filename, data.dir,
- data.referrer || "", data.size || 0);
- var _this = this;
- setTimeout(function() {
- _this.checkDownloadQueue();
- }, 0);
- } catch (e) {
- com.VidBar.__e("com.VidBar.VidDownloader.queueDownload: " + e);
- }
- },
- checkDownloadQueue : function() {
- com.VidBar.__d("com.VidBar.VidDownloader.checkDownloadQueue");
- try {
- var candicates = [];
- var pendingQueue = this.db.getWaitings();
- var downloadingLen = this.db.getDownloadingLength();
- var downloadLimit = this.pref.getMaxDownload();
- var amount = downloadLimit == 0
- ? pendingQueue.length
- : downloadLimit - downloadingLen;
- for (var i = 0; i < amount; i++) {
- if (pendingQueue.length > 0) {
- var entry = pendingQueue.shift();
- candicates.push(entry);
- }
- }
- for (var i = 0; i < candicates.length; i++) {
- this.doDownload(candicates[i]);
- }
- } catch (e) {
- com.VidBar.__e("com.VidBar.VidDownloader.checkDownloadQueue: " + e);
- }
- },
- doDownload : function(entry) {
- com.VidBar.__d("com.VidBar.VidDownloader.doDownload");
- try {
- var dwDir = this.pref.getDownloadDirectory();
- if (!dwDir.exists()) {
- dwDir
- .create(Components.interfaces.nsIFile.DIRECTORY_TYPE,
- 0775);
- }
-
- var filename = entry.filename;
- if (filename == null) {
- filename = "video.flv";
- }
- var dir = Components.classes["@mozilla.org/file/local;1"]
- .createInstance(Components.interfaces.nsILocalFile);
-
- var file;
- if (entry.dir == "") {
- file = dwDir.clone();
- }
- else {
- dir.initWithPath(entry.dir);
- file = dir.clone();
- }
- file.append(filename);
- file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
- 0644);
- var targetURI = makeFileURI(file);
-
- var sourceURI = Components.classes["@mozilla.org/network/standard-url;1"]
- .createInstance(Components.interfaces.nsIURI);
- sourceURI.spec = entry.mediaUrl;
-
- var referrerURI = null;
- if (entry.referrer) {
- referrerURI = Components.classes["@mozilla.org/network/standard-url;1"]
- .createInstance(Components.interfaces.nsIURI);
- referrerURI.spec = entry.referrer;
- }
-
- // TODO
- var shouldBypassCache = false;
-
- var persist = makeWebBrowserPersist();
-
- const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
- const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
- if (shouldBypassCache)
- persist.persistFlags = flags
- | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
- else
- persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
- persist.persistFlags |= nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
-
- var download = this.dlMgr
- .addDownload(
- Components.interfaces.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
- sourceURI, targetURI, "", null, null, null, persist);
-
- this.db.addDownloading(entry.guid, entry.mediaUrl, entry.filename, entry.dir,
- entry.referrer, entry.size, VidStatus.DOWNLOADING,
- download.id);
- this.db.removeWaiting(entry.guid);
-
- persist.progressListener = download;
-
- persist
- .saveURI(sourceURI, null, referrerURI, null, null,
- targetURI);
-
- this.repository.updateMediaByGuid(entry.guid, {
- status : VidStatus.DOWNLOADING
- });
- } catch (e) {
- com.VidBar.__e("com.VidBar.VidDownloader.doDownload: " + e);
- }
- },
- updateDownloadProgress : function(entry, download) {
- com.VidBar.__d("com.VidBar.VidDownloader.updateDownloadProgress");
- var update = {
- percentComplete : download.percentComplete,
- size : download.size,
- amountTransferred : download.amountTransferred,
- startTime : new Date(download.startTime / 1000),
- speed : download.speed
- };
- this.db.updateDownloadingProgress(entry.guid, download.size,
- download.percentComplete, download.amountTransferred,
- download.speed);
- for (var p in update) {
- entry[p] = update[p];
- }
- this.repository.updateMediaByGuid(entry.guid, update);
- },
- updateDownloadStatus : function(entry, downloadStatus) {
- com.VidBar.__d("com.VidBar.VidDownloader.updateDownloadStatus");
- entry.status = downloadStatus;
- this.db.updateDownloadingStatus(entry.guid, downloadStatus);
- this.repository.updateMediaByGuid(entry.guid, {
- status : downloadStatus
- });
- },
- transferDone : function(entry, download) {
- com.VidBar.__d("com.VidBar.VidDownloader.transferDone");
- try {
- this.repository.updateMediaByGuid(entry.guid, {
- status : VidStatus.translateState(download.state)
- });
- this.updateDownloadProgress(entry, download);
- this.updateDownloadStatus(entry, VidStatus.COMPLETE);
- this.db.removeDownloading(entry.guid);
-
- com.VidBar.__d("com.VidBar.VidDownloader.transferDone: " + entry.filename + " is done.");
-
- var _this = this;
- setTimeout(function() {
- _this.checkDownloadQueue();
- }, 0);
- } catch (e) {
- com.VidBar.__e("com.VidBar.VidDownloader.transferDone: " + e);
- }
- },
- removeDownloads : function(entries) {
- com.VidBar.__d("com.VidBar.VidDownloader.removeDownloads");
- },
- syncDownloads : function(){
- com.VidBar.__d("com.VidBar.VidDownloader.syncDownloads");
- this.db.syncDownloads(this.dlMgr);
- },
- doCloseCheck : function(){
- com.VidBar.__d("com.VidBar.VidDownloader.doCloseCheck");
- var msg = null;
- var length = 0;
- var downloadings = this.db.getDownloadings();
- for (var i in downloadings){
- if(downloadings[i].status == VidStatus.DOWNLOADING) {
- length++;
- }
- }
- if (length == 1) {
- msg = "There is still 1 video in downloading. Are you sure you want to stop it and close the browser?";
- } else if (length > 1) {
- msg = "There are still "
- + length
- + " videos in downloading. Are you sure you want to stop it and close the browser?";
- } else {
- var waitings = this.db.getWaitings();
- if (waitings.length == 1) {
- msg = "There is still 1 video in queue. Are you sure you want to stop it and close the browser?";
- } else if (waitings.length > 1) {
- msg = "There are still "
- + waitings.length
- + " videos in queue. Are you sure you want to stop it and close the browser?";
- }
- }
- var result = {cancel:false, clear:false};
- if(msg){
- window.openDialog("chrome://vidbar/content/exitdialog.xul", "",
- "chrome, dialog, modal, resizable=no, centerscreen=true", msg, result).focus();
- }
- return result;
- },
- cleanAll : function(){
- com.VidBar.__d("com.VidBar.VidDownloader.cleanAll");
- this.db.removeAllWaitings();
- var downloadings = this.db.getDownloadings();
- for(var i in downloadings){
- com.VidBar.__d("downloadId: "+downloadings[i].downloadId);
- try{
- this.dlMgr.cancelDownload(downloadings[i].downloadId);
- }catch(ex){
- com.VidBar.__e("com.VidBar.VidDownloader.cleanAll: " + ex);
- }
- try{
- this.dlMgr.removeDownload(downloadings[i].downloadId);
- }catch(ex){
- com.VidBar.__e("com.VidBar.VidDownloader.cleanAll: " + ex);
- }
- }
- this.db.removeAllDownloadings();
- }
- };
-
- com.VidBar.Main = {
- analyzers : {},
- netMonitor : null,
-
- init : function() {
- com.VidBar.__d("com.VidBar.Main.init");
- this.analyzers = {
- "youtube" : new com.VidBar.YouTubeAnalyzer(com.VidBar.Pref),
- "medialink" : new com.VidBar.LinkAnalyzer(com.VidBar.Pref)
- }
-
- this.netMonitor = new com.VidBar.NetMonitor(com.VidBar.Repository, com.VidBar.Pref);
-
- var container = gBrowser.mPanelContainer;
- container.addEventListener("select", function(event) {
- com.VidBar.Main.onTabSelected(event);
- }, false);
- window.addEventListener("pageshow", function(event) {
- com.VidBar.Main.onPageShow(event);
- }, true);
- window.addEventListener("pagehide", function(event) {
- com.VidBar.Main.onPageHide(event);
- }, true);
- },
- onLoad : function(event) {
- com.VidBar.__d("com.VidBar.Main.onLoad");
- com.VidBar.Observer.register();
- com.VidBar.Pref.init();
- com.VidBar.UI.init(com.VidBar.Pref);
- com.VidBar.Repository.init();
- com.VidBar.VidDownloader.init(com.VidBar.Pref, com.VidBar.Repository);
- com.VidBar.Main.init();
- com.VidBar.Repository.addObserver(com.VidBar.UI);
- com.VidBar.VidFB.init(com.VidBar.Pref);
- com.VidBar.Main.checkFirstStart();
-
- },
- onClose : function(event) {
- com.VidBar.__d("com.VidBar.Main.onClose");
- //com.VidBar.Pref.close();
- //com.VidBar.VidDownloader.close();
- },
- onPageShow : function(event) {
- com.VidBar.__d("com.VidBar.Main.onPageShow");
- var doc = event.originalTarget;
- if (doc instanceof HTMLDocument) {
- // com.VidBar.__d("com.VidBar.Main.onPageShow:\ndoc: " + doc.URL);
-
- var list = [];
- for (var i in this.analyzers) {
- var analyzer = this.analyzers[i];
- try {
- list = list.concat(analyzer.analyzeDoc(doc));
- } catch (e) {
- com.VidBar.__e(analyzer.name + " error during analyzing document: "
- + e);
- }
- }
- com.VidBar.Repository.addMediaListByDoc(list, doc);
- }
- },
- onPageHide : function(event) {
- com.VidBar.__d("com.VidBar.Main.onPageHide");
- var doc = event.originalTarget;
- if (doc instanceof HTMLDocument) {
- // com.VidBar.__d("com.VidBar.Main.onPageHide: doc: " + doc.URL);
-
- com.VidBar.Repository.removeMediaByDoc(doc);
- }
- },
- onTabSelected : function(event) {
- com.VidBar.__d("com.VidBar.Main.onTabSelected");
- com.VidBar.UI.update(com.VidBar.Repository);
- },
- checkFirstStart : function() {
- try {
- var isFirstStart = com.VidBar.Pref.isFirstTime();
- com.VidBar.__d("com.VidBar.Main.checkFirstStart: "+isFirstStart);
- com.VidBar.ServerPings.onInstall();
- if (isFirstStart) {
- com.VidBar.Pref.unsetFirstTime();
- com.VidBar.ServerPings.onInstall();
- var OSName = "Unknown OS";
- if (navigator.appVersion.indexOf("Win") != -1)
- OSName = "Windows";
- if (navigator.appVersion.indexOf("Mac") != -1)
- OSName = "MacOS";
- if (navigator.appVersion.indexOf("X11") != -1)
- OSName = "UNIX";
- if (navigator.appVersion.indexOf("Linux") != -1)
- OSName = "Linux";
-
- if (OSName == "MacOS") {
- var url = "http://vids.st/mac/success.html";
- setTimeout(function() {
- com.VidBar.UI.openURLInNewTab(url);
- }, 2);
- } else
- // if(OSName == "Windows")
- {
- var url = "http://vids.st/success.html";
- setTimeout(function() {
- com.VidBar.UI.openURLInNewTab(url);
- }, 2);
- }
- }
- } catch (e) {
- com.VidBar.__e("com.VidBar.Main.checkFirstStart: " + e);
- }
- }
- };
-